package com.lstimeline.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

import com.lstimeline.utility.Validation;
import com.lstimeline.utility.DataManager.RegisterDataManager;

public class RegisterServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		RegisterDataManager.loadDriver();
	}
	
	//A helper method that forwards the request to register.jspx
	private void redirectToViewer(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/register.jspx");
		dispatcher.forward(request, response);
	}
	
	//A helper method that handles register error
	private void registerError(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		String errorMessage = "Registration failed. Please make sure your inputs are compliant to all the rules and try again.";
		request.setAttribute("errorMessage", errorMessage);
		redirectToViewer(request,response);
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		redirectToViewer(request,response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		String username = request.getParameter("martinet");
		String password = request.getParameter("animalcule");
		String email = request.getParameter("email");
		if(!Validation.isUsernameWellFormed(username) || 
		   !Validation.isPasswordWellFormed(password) ||
		   !Validation.isEmailWellFormed(email)) {
			//System.out.println("Register input abnormality detected.");
			registerError(request, response);
		} else if(!RegisterDataManager.isUsernameAvailable(username) ||
				  !RegisterDataManager.isEmailUnused(email)) {
			//TODO Debug only, this secion to be replaced by AJAX functions later
			//System.out.println("Duplicate records detected.");
			registerError(request, response);
		} else {
			boolean isSuccessful = RegisterDataManager.registerUser(username, password, email);
			if(isSuccessful) {
				//TODO Create Session, Redirect to member page
				response.setContentType("text/html");
				PrintWriter out = response.getWriter();
				
				out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
	                    "Transitional//EN\">\n" +
	                    "<HTML>\n" +
	                    "<HEAD><TITLE>User Data</TITLE></HEAD>\n" +
	                    	"<BODY>\n" +
	                    		"<div>" +
	                    			" The registration is successful! Please click "+
	                    			"<a href=\"login\">here</a> to continue."+
	                    		"</div>\n" +
						"</BODY></HTML>");
				
				out.close();
			} else {
				//System.out.println("Unexpected registration error.");
				registerError(request, response);
			}
		}
	}
}
